package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.annotation.Target;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common/upload")
@Api(tags = "公用接口")
public class FileUplodeController {
    @Autowired
    AliOssUtil aliOssUtil;
   /* @Value("${aliyun.oss.bucketName}")
    private String buchetname;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;*/

    @PostMapping
    @ApiOperation("文件上传")
    public Result uplode(MultipartFile file) throws IOException {

       /* aliOssUtil.setBucketName(this.buchetname);
        aliOssUtil.setEndpoint(this.endpoint);*/
        /*String name = file.getName();*/
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);
        String string = UUID.randomUUID().toString();
        String objectName = string + suffix;
        byte[] bytes = file.getBytes();
        String upload = aliOssUtil.upload(bytes, objectName);
        return Result.success(upload);

    }

}
